var webPack = require("webpack");
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var glob = require('glob');
var metadata = {
    //host: "http://weixin.evcardchina.com/h5app",
    host: '/assets',
    version: "2017010302"
};
module.exports = {
    entry: {
        index: [
            './src/index.js'
        ]
    },
    output: {
        path: path.resolve(__dirname, './assets/'),
        //publicPath: 'http://weixin.evcardchina.com/h5app/',		
        publicPath: '/assets/',
        filename: "[name].js",
        chunkFilename: 'chunk/[name].js'
    },
    module: {
        rules: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }) },
            { test: /\.less$/, loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader!less-loader' }) },
            { test: /\.(png|jpg)$/, loader: "url-loader?limit=10000&name=./img/[name].[ext]" },
            { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader", query: { presets: ["es2015"] } },
            { test: /\.(woff|woff2|eot|ttf|svg|otf|ttc)(\?.*$|$)/, loader: 'file-loader?importLoaders=1&limit=1000&name=/css/fonts/[name].[ext]' },
            { test: /\.json$/, loader: 'json' },
            { test: /\.vue$/, loader: 'vue-loader' }
        ]
    },
    plugins: [
        //把入口文件里面的数组打包成verdors.js
        new webPack.optimize.CommonsChunkPlugin('common'),
        new ExtractTextPlugin("css/[name].css"),
        new webPack.DefinePlugin({
            metadata: JSON.stringify(metadata),
        }),
        new HtmlWebpackPlugin({
            filename: './index.html', //生成的html存放路径，相对于 path
            template: './src/index.html', //html模板路径
            inject: false, //允许插件修改哪些内容，包括head与body
            hash: true,
        })
    ].concat(getHtmlPlugin())
}

function getHtmlPlugin() {
    var plugins = [];
    glob.sync(process.cwd() + '/src/page/*.html').forEach(function(name) {
        var n = name.match(/([^/]+?)\.html/)[1];
        plugins.push(
            new HtmlWebpackPlugin({
                filename: '/page/' + n + '.html',
                template: name,
            })
        )
    });
    // entry['a'] = 'webpack-dev-server/client?http://localhost:3000';
    // entry['b'] = 'webpack/hot/only-dev-server';
    return plugins;
}